package com.zhonglz.api.px;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.util.Random;

import javax.swing.JFrame;

public class MainFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
	public static final int SNOW_NUM = 400;
	public static final int SPEED = 10;
	private static Color[] color={Color.BLACK,Color.BLUE,Color.CYAN,Color.GREEN,       //定义颜色数组
			Color.ORANGE,Color.YELLOW,Color.RED,Color.PINK,Color.LIGHT_GRAY};
	private static final Random rand = new Random();   //创建随机对象
	int[] x=new int[]{150,300,450,600,750,900,1050,1200};
	int[] y=new int[]{0,-100,-200,-300,-400,-500,-600,-700};
	//String[] str=new String[]{"我","的","小","可","爱","在","么","!"};
	private int[] xs = new int[scrSize.width];
	private int[] ys = new int[scrSize.height];
	private Font[] snows = new Font[10];

	public MainFrame() {
		initSnow();// 初始化‘雪花’
		setUndecorated(true);
		setBackground(new Color(0, 0, 0, 0));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	private static Color getC(){      //获取随机颜色值的方法
		return color[rand.nextInt(color.length)];
		}
	//判断飘雪
	public void sta(int s){
		if(s==0){
			fallSnow();//启动线程
		}else{
			System.out.println("请携带参数0开始飘雪");
		}
	}

	public void initSnow() {
		Random rd = new Random();
		for (int i = 0; i < SNOW_NUM; i++) {
			xs[i] = rd.nextInt(scrSize.width);
			ys[i] = rd.nextInt(scrSize.height);
		}
		for (int i = 0; i < snows.length; i++) {
			snows[i] = new Font("宋体", Font.PLAIN, 15 + i);
		}
	}
	public void paint(Graphics g) {
		super.paint(g);
		Color c = g.getColor();
		g.setColor(Color.WHITE);
		for (int i = 0; i < SNOW_NUM; i++) {
			g.setFont(snows[i % snows.length]);
			g.drawString("*", xs[i], ys[i]);
		}
//		for (int i = 0; i < str.length; i++) {
//			g.setColor(getC());
//			g.drawString(str[i], x[i], y[i]);
//		}
		g.setColor(c);
	}

	public void fallSnow() {
		new Thread() {
			public void run() {
				while (true) {
					for (int i = 0; i < SNOW_NUM; i++) {
						//飘雪
						ys[i]++;
						if (ys[i] > scrSize.height) {
							ys[i] = 0;
						}
					}
//					for (int j = 0; j < str.length; j++) {
//						y[j]++;
//						if (y[j]>=scrSize.height/2) {
//							y[j]=scrSize.height/2;
//						}
//					}
					try {
						Thread.sleep(SPEED);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					repaint();
				}
			}
		}.start();
	}
}